package com.android.vgongyi.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

import com.android.vgongyi.utils.AsyncImageLoader;

public class WaterView extends ImageView implements OnClickListener,OnLongClickListener{
	private static final String TAG ="WaterView";
	private Context context;
	private WaterTag waterTag;
	private int columnIndex;
	private int rowIndex;
	private Handler viewHandler;
	private Drawable drawable;
	public Bitmap bitmap;
	public static int threadCount = 0;
	public WaterView(Context c, AttributeSet attrs, int defStyle) {
		super(c, attrs, defStyle);
		this.context = c;
		Init();
	}

	public WaterView(Context c, AttributeSet attrs) {
		super(c, attrs);
		this.context = c;
		Init();
	}

	public WaterView(Context c) {
		super(c);
		this.context = c;
		Init();
	}
	
	private void Init() {
		setOnClickListener(this);
		setOnLongClickListener(this);
		setAdjustViewBounds(true);
	}

	@Override
	public boolean onLongClick(View v) {
		Log.i(TAG,"onLongClick");
		return false;
	}

	@Override
	public void onClick(View v) {
		Log.i(TAG,"onClick");
	}
	
	public void LoadImage() {
		if (getFlowTag() != null) {
			new LoadImageThread().start();
		}
	}

	public void Reload() {
		if (this.bitmap == null && getFlowTag() != null) {
			new ReloadImageThread().start();
		}
	}

	public void recycle() {
		setImageBitmap(null);
		if ((this.bitmap == null) || (this.bitmap.isRecycled()))
			return;
		this.bitmap.recycle();
		this.bitmap = null;
	}
	
	public WaterTag getFlowTag() {
		return waterTag;
	}

	public void setFlowTag(WaterTag waterTag) {
		this.waterTag = waterTag;
	}

	public int getColumnIndex() {
		return columnIndex;
	}

	public void setColumnIndex(int columnIndex) {
		this.columnIndex = columnIndex;
	}

	public int getRowIndex() {
		return rowIndex;
	}

	public void setRowIndex(int rowIndex) {
		this.rowIndex = rowIndex;
	}

	public Handler getViewHandler() {
		return viewHandler;
	}

	public WaterView setViewHandler(Handler viewHandler) {
		this.viewHandler = viewHandler;
		return this;
	}

	class ReloadImageThread extends Thread {

		@Override
		public void run() {
			threadCount++;
			Looper.prepare();
			if (waterTag != null) {

//				BufferedInputStream buf;
//				try {
//					buf = new BufferedInputStream(new FileInputStream(new File(AppConstant.IMAGE_PATH,waterTag.getFileName())));
//					bitmap = BitmapFactory.decodeStream(buf);
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
				drawable  = AsyncImageLoader.loadImageFromUrl(context, waterTag.getImg_url(), waterTag.getImg_extension());
				bitmap = getBitmapFromDrawable(drawable);
				((Activity) context).runOnUiThread(new Runnable() {
					public void run() {
						if (bitmap != null) {// 此处在线程过多时可能为null
							setImageBitmap(bitmap);
						}
					}
				});
			}
			threadCount--;
			Looper.loop();
		}
	}

	class LoadImageThread extends Thread {
		LoadImageThread() {}

		public void run() {
			threadCount ++;
			Looper.prepare();
			if (waterTag != null) {
				drawable  = AsyncImageLoader.loadImageFromUrl(context, waterTag.getImg_url(), waterTag.getImg_extension());
				bitmap = getBitmapFromDrawable(drawable);
				((Activity) context).runOnUiThread(new Runnable() {
					public void run() {
						if (bitmap != null) {// 此处在线程过多时可能为null
							int width = bitmap.getWidth();// 获取真实宽高
							int height = bitmap.getHeight();
							LayoutParams lp = getLayoutParams();
							int layoutHeight = (height * waterTag.getItemWidth()) / width;// 调整高度
							if (lp == null) {
								lp = new LayoutParams(waterTag.getItemWidth(),layoutHeight);
							}
							setLayoutParams(lp);
							setImageBitmap(bitmap);
							Handler h = getViewHandler();
							Message m = h.obtainMessage(waterTag.what, width,layoutHeight, WaterView.this);
							h.sendMessage(m);
						}
					}
				});
			}
			threadCount--;
			Looper.loop();
		}
	}
	public static Bitmap getBitmapFromDrawable(Drawable drawable){
	    Bitmap bitmap = null;
	    try {
	        BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;
	        bitmap = bitmapDrawable.getBitmap();

	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return bitmap;
	}
}
